﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：检验依据管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;

public partial class GBInfo : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaGbinfo dalGb = new DaGbinfo();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            Utility.BindDataSet2Combox(dalGb.Query(string.Empty), DDlGB_Code, "GB_Title", "GB_Code");
            this.DDlGB_Code_SelectedIndexChanged(null, null);
        }
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoGbinfo mod=new MoGbinfo();
        try
        {
            mod.Gb_Code=EditGB_Code.Text.Trim();
            mod.Gb_Title=EditGB_Title.Text.Trim();
            mod.Gb_Subject=EditGB_Subject.Text.Trim();
            mod.Gb_Status = int.Parse(EditGB_Status.SelectedValue);
            mod.Rem=EditRem.Text.Trim();

            int rtn = dalGb.Update(mod);

            if (rtn == 1)
            {
                Utility.BindDataSet2Combox(dalGb.Query(string.Empty), DDlGB_Code, "GB_Title", "GB_Code");
                OperationResult.Text = "修改检验依据成功！"; divResult.Visible = true;
                this.DDlGB_Code_SelectedIndexChanged(null, null);
            }
            else
            {
                OperationResult.Text = "修改检验依据失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("修改检验依据失败", ex);
            OperationResult.Text = "修改检验依据失败！"; divResult.Visible = true;
        }
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(DetailPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        if (dalGb.IsExist(CreateGB_Code.Text.Trim()))
        {
            OperationResult.Text = "检验依据编码重复！"; divResult.Visible = true;
            return;
        }
        try
        {
            MoGbinfo mod=new MoGbinfo();
            mod.Gb_Code = CreateGB_Code.Text.Trim();
            mod.Gb_Title = CreateGB_Title.Text.Trim();
            mod.Gb_Subject = CreateGB_Subject.Text.Trim();
            mod.Gb_Status = int.Parse(CreateGB_Status.SelectedValue);
            mod.Rem = CreateRem.Text.Trim();

            int rtn = dalGb.Add(mod);

            if (rtn == 1)
            {
                Utility.BindDataSet2Combox(dalGb.Query(string.Empty), DDlGB_Code, "GB_Title", "GB_Code");
                OperationResult.Text = "创建新检验依据成功！"; divResult.Visible = true;
                this.DDlGB_Code_SelectedIndexChanged(null, null);
            }
            else
            {
                OperationResult.Text = "创建新检验依据失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("创建新检验依据失败", ex);
            OperationResult.Text = "创建新检验依据失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(DetailPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailGB_Code.Text.Trim();

        MoGbinfo mod = dalGb.GetEntity(strKey);

        if (mod != null)
        {
            EditGB_Code.Text = mod.Gb_Code;
            EditGB_Title.Text = mod.Gb_Title;
            EditGB_Subject.Text = mod.Gb_Subject;
            EditGB_Status.SelectedValue = mod.Gb_Status.ToString();
            EditRem.Text = mod.Rem;
        }

        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailGB_Code.Text.Trim();

        try
        {
            int rtn = dalGb.Delete(strKey);
            if (rtn == 1)
            {
                Utility.BindDataSet2Combox(dalGb.Query(string.Empty), DDlGB_Code, "GB_Title", "GB_Code");
                OperationResult.Text = "删除检验依据成功！"; divResult.Visible = true;
                this.DDlGB_Code_SelectedIndexChanged(null, null);
            }
            else
            {
                OperationResult.Text = "删除检验依据失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("删除检验依据失败", ex);
            OperationResult.Text = "删除检验依据失败！"; divResult.Visible = true;
        }
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;

        panel.Visible = true;
    }

    protected int DeleteInfo(string strKey)
    {
        return dalGb.Delete(strKey);
    }

    protected void DisplayDetailInfo(string strKey)
    {
        //这里查询信息
        MoGbinfo mod = dalGb.GetEntity(strKey);

        //然后给查看视图赋值。
        DetailGB_Code.Text=mod.Gb_Code;
        DetailGB_Title.Text=mod.Gb_Title;
        DetailGB_Subject.Text=mod.Gb_Subject;
        DetailGB_Status.Text = mod.Gb_Status == 0 ? "在用" : "作废"; 
        DetailRem.Text=mod.Rem;
    }

    protected void CreateCancel()
    {
        CreateGB_Code.Text = "";
        CreateGB_Title.Text = "";
        CreateGB_Subject.Text = "";
        CreateGB_Status.SelectedIndex = 0;
        CreateRem.Text = "";
    }

    protected void DDlGB_Code_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.SetPanelVisiable(DetailPanel);
        MoGbinfo mod = dalGb.GetEntity(DDlGB_Code.SelectedValue);

        if (mod != null)
        {
            DetailGB_Code.Text = mod.Gb_Code;
            DetailGB_Title.Text = mod.Gb_Title;
            DetailGB_Subject.Text = mod.Gb_Subject;
            DetailGB_Status.Text = mod.Gb_Status == 0 ? "在用" : "作废";
            DetailRem.Text = mod.Rem;
        }
    }
}
